﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.Plumbing
{
	internal class SprinklerSectorJig : XUiJigBase
	{
		public SprinklerSectorJig(UIDocument uiDoc) : base(uiDoc)
		{
		}

		public bool SectorPoint(SectorLaySprinkler sec, UserSettedInfo set)
		{
			bool result;
			try
			{
				this.sector = sec;
				this.userSet = set;
				this.selPos = SprinklerSectorJig.SelPointPos.eStart;
				this.sector.sPoint = base.UiDoc.Selection.PickPoint("选择起点……");
				this.selPos = SprinklerSectorJig.SelPointPos.eEnd;
				this.sector.ePoint = base.UiDoc.Selection.PickPoint("选择终点……");
				this.selPos = SprinklerSectorJig.SelPointPos.eOn;
				this.sector.onPoint = base.UiDoc.Selection.PickPoint("弧线上一点……");
				string message = "";
				bool flag = this.sector.CheckPoints(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint, ref message);
				while (!flag)
				{
					YJKMessageBox.Show(message);
					this.selPos = SprinklerSectorJig.SelPointPos.eStart;
					this.sector.sPoint = base.UiDoc.Selection.PickPoint("选择起点……");
					this.selPos = SprinklerSectorJig.SelPointPos.eEnd;
					this.sector.ePoint = base.UiDoc.Selection.PickPoint("选择终点……");
					this.selPos = SprinklerSectorJig.SelPointPos.eOn;
					this.sector.onPoint = base.UiDoc.Selection.PickPoint("弧线上一点……");
					message = "";
					flag = this.sector.CheckPoints(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint, ref message);
				}
				this.selPos = SprinklerSectorJig.SelPointPos.eNo;
				result = true;
			}
			catch
			{
				this.selPos = SprinklerSectorJig.SelPointPos.eNo;
				result = false;
			}
			return result;
		}

		public bool underPoint()
		{
			bool result;
			try
			{
				this.selPos = SprinklerSectorJig.SelPointPos.eUnder;
				this.sector.unPoint = base.UiDoc.Selection.PickPoint("选择扇形布置区域下限点……");
				this.sector.SetPointZValue(ref this.sector.unPoint, this.sector.GetActiveViewZeroLevle());
				this.selPos = SprinklerSectorJig.SelPointPos.eNo;
				this.GetPoints();
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			System.Drawing.Point ptCur = this.m_ptCur;
			if (this.m_ptCur.IsEmpty || paintEventArgs == null)
			{
				return;
			}
			if (this.selPos == SprinklerSectorJig.SelPointPos.eNo || this.selPos == SprinklerSectorJig.SelPointPos.eStart || this.sector == null || this.userSet == null)
			{
				return;
			}
			try
			{
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				if (xyz != null)
				{
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					if (this.selPos == SprinklerSectorJig.SelPointPos.eEnd)
					{
						new XLine2D(base.Revit2Client(this.sector.sPoint), this.m_ptCur).Draw(g);
					}
					else if (this.selPos == SprinklerSectorJig.SelPointPos.eOn)
					{
						System.Drawing.Point point = base.Revit2Client(this.sector.sPoint);
						System.Drawing.Point ptEnd = base.Revit2Client(this.sector.ePoint);
						System.Drawing.Point ptCur2 = this.m_ptCur;
						if (!this.sector.sPoint.IsAlmostEqualTo(this.sector.ePoint, 0.001) && !xyz.IsAlmostEqualTo(this.sector.sPoint, 0.001) && !xyz.IsAlmostEqualTo(this.sector.ePoint, 0.001))
						{
							Graphics graphics = paintEventArgs.Graphics;
							new XArc2D(point, ptEnd, ptCur2).Draw(g);
							XYZ center = base.UiDoc.Document.CreatYJKArc(this.sector.sPoint, this.sector.ePoint, xyz).Center;
							new XLine2D(base.Revit2Client(center), point).Draw(g);
							new XLine2D(base.Revit2Client(center), ptEnd).Draw(g);
						}
					}
					else if (this.selPos == SprinklerSectorJig.SelPointPos.eUnder)
					{
						System.Drawing.Point point2 = base.Revit2Client(this.sector.sPoint);
						System.Drawing.Point ptEnd2 = base.Revit2Client(this.sector.ePoint);
						System.Drawing.Point ptOnArc = base.Revit2Client(this.sector.onPoint);
						this.sector.unPoint = xyz;
						Graphics graphics2 = paintEventArgs.Graphics;
						new XArc2D(point2, ptEnd2, ptOnArc).Draw(g);
						XYZ center2 = base.UiDoc.Document.CreatYJKArc(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint).Center;
						if (this.sector.CheckPntInSector(this.sector.unPoint) && !this.sector.unPoint.IsAlmostEqualTo(this.sector.CenterPnt, 0.0001))
						{
							YJKCurve hycurve = new YJKCurve(Arc.Create(center2, this.sector.unPoint.DistanceTo(this.sector.CenterPnt), 0.0, Math.PI*2, XYZ.BasisX, XYZ.BasisY));
							XYZ xyz2 = hycurve.IntersectionPoint(Line.CreateBound(center2, this.sector.sPoint));
							XYZ xyz3 = hycurve.IntersectionPoint(Line.CreateBound(center2, this.sector.ePoint));
							if (xyz2 != null && xyz3 != null)
							{
								new XArc2D(base.Revit2Client(xyz2), base.Revit2Client(xyz3), this.m_ptCur).Draw(g);
								new XLine2D(base.Revit2Client(xyz2), point2).Draw(g);
								new XLine2D(base.Revit2Client(xyz3), ptEnd2).Draw(g);
							}
						}
						else
						{
							new XLine2D(base.Revit2Client(center2), point2).Draw(g);
							new XLine2D(base.Revit2Client(center2), ptEnd2).Draw(g);
						}
						this.GetPoints();
						foreach (List<XYZ> list in this.xyzList)
						{
							if (list != null)
							{
								foreach (XYZ point3 in list)
								{
									new XCircle2D(base.Revit2Client(point3), this.nRadius).Draw(g);
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private void GetPoints()
		{
			try
			{
				double offSet = this.userSet.OffSet * 1000.0 / 304.8;
				if (this.userSet.LimitedSector == "distance")
				{
					int sectorMaxRow = this.userSet.SectorMaxRow;
					int sectorMaxCol = this.userSet.SectorMaxCol;
					int sectorMinDis = this.userSet.SectorMinDis;
					int sectorMaxSideRow = this.userSet.SectorMaxSideRow;
					int sectorMaxSideCol = this.userSet.SectorMaxSideCol;
					int divisor = this.userSet.Divisor;
					if (this.sector.CheckPntInSector(this.sector.unPoint) && !this.sector.unPoint.IsAlmostEqualTo(this.sector.CenterPnt, 0.0001))
					{
						this.xyzList = this.sector.GetLayPoints(this.sector.unPoint, sectorMaxRow, sectorMaxCol, sectorMinDis, sectorMaxSideRow, sectorMaxSideCol, divisor, offSet);
					}
					else
					{
						this.xyzList = this.sector.GetLayPoints(this.sector.CenterPnt, sectorMaxRow, sectorMaxCol, sectorMinDis, sectorMaxSideRow, sectorMaxSideCol, divisor, offSet);
					}
				}
				else
				{
					int sectorRow = this.userSet.SectorRow;
					int sectorCol = this.userSet.SectorCol;
					double sectorRatio = this.userSet.SectorRatio;
					int divisor2 = this.userSet.Divisor;
					if (this.sector.CheckPntInSector(this.sector.unPoint))
					{
						this.xyzList = this.sector.GetLayPoints(this.sector.unPoint, sectorRow, sectorCol, sectorRatio, divisor2, offSet);
					}
					else
					{
						this.xyzList = this.sector.GetLayPoints(this.sector.CenterPnt, sectorRow, sectorCol, sectorRatio, divisor2, offSet);
					}
				}
			}
			catch
			{
				this.xyzList = new List<List<XYZ>>();
			}
		}

		private int nRadius = 3;

		private SprinklerSectorJig.SelPointPos selPos;

		private SectorLaySprinkler sector;

		private UserSettedInfo userSet;

		public List<List<XYZ>> xyzList = new List<List<XYZ>>();

		private enum SelPointPos
		{
			eNo,
			eStart,
			eEnd,
			eOn,
			eUnder
		}
	}
}
